from rest_framework import serializers
from .models import *


# 用户序列化器
class UserSer(serializers.ModelSerializer):        # 只用于序列化输入 # 去掉两边的空格
    password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField(read_only=True)

    def get_role_name(self, obj):
        return obj.role.name

    class Meta:
        model = UserModel
        fields = ["id", "nickname", "avatar_url", "mobile", "password", "role", "role_name", "date_joined","last_login","is_staff"]
        read_only_fields = ["id","date_joined"]


# 地址序列化器
class AddressSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()
    def get_user_name(self, obj):
        return obj.user.nickname

    class Meta:
        model = AddressModel
        fields = "__all__"


# 购物车
class UserShoppingSer(serializers.ModelSerializer):
    # 展示所属用户
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()

    def get_user_name(self, obj):
        return obj.user.nickname

    # 展示所属商家
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()

    def get_shops_name(self, obj):
        return obj.shops.nickname

    # 展示所属商品
    goods = serializers.PrimaryKeyRelatedField(queryset=GoodsModel.objects.all())
    goods_name = serializers.SerializerMethodField()

    def get_goods_name(self, obj):
        return obj.goods.name

    class Meta:
        model = UserShoppingModel
        fields = "__all__"


# 收藏
class CollectSer(serializers.ModelSerializer):
    # 展示所属用户
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()

    def get_user_name(self, obj):
        return obj.user.nickname

    # 展示所属商家
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()

    def get_shops_name(self, obj):
        return obj.shops.nickname

    # def get_goods_name(self, obj):
    #     return obj.goods.name

    class Meta:
        model = CollectModel
        fields = "__all__"


# 订单
class OrderSer(serializers.ModelSerializer):
    pay_state = serializers.SerializerMethodField()
    def get_pay_state(self, obj):
        if obj.pay_state == 0:
            pay_state = "未支付"
            return pay_state
        elif obj.pay_state == 1:
            pay_state = "已支付"
            return pay_state

    # 展示所属用户
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()

    def get_user_name(self, obj):
        return obj.user.nickname

    # 展示所属商家
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()

    def get_shops_name(self, obj):
        return obj.shops.nickname

    # 展示所属商品
    goods = serializers.PrimaryKeyRelatedField(queryset=GoodsModel.objects.all())
    goods_name = serializers.SerializerMethodField()

    def get_goods_name(self, obj):
        return obj.goods.name

    # 展示地址
    address = serializers.PrimaryKeyRelatedField(queryset=AddressModel.objects.all())
    address_name = serializers.SerializerMethodField()

    def get_address_name(self, obj):
        return obj.address.address


    class Meta:
        model = OrderModel
        fields = "__all__"


# 商家订单
class OrderGoodsSer(serializers.ModelSerializer):

    # 展示所属商品
    goods = serializers.PrimaryKeyRelatedField(queryset=GoodsModel.objects.all())
    goods_name = serializers.SerializerMethodField()

    def get_goods_name(self, obj):
        return obj.goods.name

    class Meta:
        model = OrderGoods
        fields = "__all__"


# 评论
class CommentSer(serializers.ModelSerializer):
    # 展示所属用户
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField()

    def get_user_name(self, obj):
        return obj.user.nickname

    # 展示所属商家
    shops = serializers.PrimaryKeyRelatedField(queryset=ShopsModel.objects.all())
    shops_name = serializers.SerializerMethodField()

    def get_shops_name(self, obj):
        return obj.shops.nickname

    # 所属订单
    order = serializers.PrimaryKeyRelatedField(queryset=OrderModel.objects.all())

    class Meta:
        model = CommentModel
        fields = "__all__"


